﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace UNBP.VBPQ.RenderService
{
    public class VBPQBussinessProcessBll
    {
        public static CmsDataCls[] SearchData(string SiteId, string Alias, string Keyword, string FromDate, string ToDate, int ActiveSortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = "    select  TableData.* from TableData " +
                           "    left outer join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
                           "    where IsPublic = @IsPublic and Alias=@Alias and ParentDataId is null ";
            if (!string.IsNullOrEmpty(Keyword))
            {
                Query += " and (Subject like N'%" + Keyword + "%' or Description like N'%" + Keyword + "%' or Body like N'%" + Keyword + "%' )";
            }
            if (!string.IsNullOrEmpty(FromDate) && !string.IsNullOrEmpty(ToDate))
            {
                DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
                DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
                //Query += " and TableData.EntryDate between '" + dFromDate.ToString("MM/dd/yyyy 00:00") + "' and '" + dToDate.ToString("MM/dd/yyyy 23:59") + "'";
                Query += " and TableData.EntryDate between @fromDate and @toDate ";
            }
            if (ActiveSortIndex == 1)
            {
                Query +=
                           "        order by TableData.SortIndex ";
            }
            else
            {
                Query +=
                           "        order by EntryDate DESC";
            }
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("Alias",Alias),
                        new DbParam("IsPublic",1),
                        new DbParam("fromDate",FunctionUtilities.VNDateToDate(FromDate),DbType.DateTime),
                        new DbParam("toDate",FunctionUtilities.VNDateToDate(ToDate),DbType.DateTime),                        

                    });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new CmsDataCls[0];
            }

            CmsDataCls[] CmsDatas = CmsDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsDatas;
        }


        public static string[] ReadingDocumentNew(string SiteId, string GroupDataId)
        {
            string TopN = "20";
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            try
            {
                TopN = WebConfig.GetConfigFromXml(OSiteParam, "TopNewRecord");
                if (string.IsNullOrEmpty(TopN)) TopN = "20";
            }
            catch { TopN = "20"; }
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = "    select TOP " + TopN + " DataAlias from TableData " +
                           "    where IsPublic = @IsPublic and frkGroupDataId=@GroupDataId and ParentDataId is null " +
                           "    order by EntryDate DESC";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("GroupDataId",GroupDataId),
                        new DbParam("IsPublic",1)
                    });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new string[0];
            }
            string[] DocumentNo = new string[dsResult.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < DocumentNo.Length; iIndex++)
            {
                DocumentNo[iIndex] = (string)dsResult.Tables[0].Rows[iIndex]["DataAlias"];
            }
            dsResult.Clear();
            dsResult.Dispose();
            return DocumentNo;
        }
    }
}
